package cn.emall.filter;

import cn.emall.config.FilterProperties;
import cn.emall.config.JwtProperties;
import com.alibaba.fastjson.JSON;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import token.common.pojo.UserInfo;
import token.common.util.JwtUtil;
import utils.CookieUtil;
import utils.ResponseBean;
import utils.ResultCode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@EnableConfigurationProperties({JwtProperties.class, FilterProperties.class})
public class TokenFilter extends ZuulFilter {

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private FilterProperties filterProperties;

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 10;
    }

    @Override
    public boolean shouldFilter() {
        // 获取上下文
        RequestContext ctx = RequestContext.getCurrentContext();
        // 获取request
        HttpServletRequest req = ctx.getRequest();
        // 获取路径
        String requestURI = req.getRequestURI();
        // 判断白名单
        // 遍历允许访问的路径
        for (String path : this.filterProperties.getAllowPaths()) {
            // 然后判断是否是符合
            if(requestURI.startsWith(path)){
                return false;
            }
        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {

        // 获取上下文
        RequestContext context = RequestContext.getCurrentContext();
        // 获取request
        HttpServletRequest request = context.getRequest();
        HttpServletResponse response=context.getResponse();
        // 获取token
        String token = CookieUtil.getCookieValue(request, this.jwtProperties.getCookieName());
       // String token = request.getHeader("EMALL_TOKEN");
        System.out.println("获取token!!!");
        System.out.println(token);
        if(token==null){
            context.setSendZuulResponse(false);
            String str= JSON.toJSONString(ResponseBean.failure(ResultCode.USER_NOT_LOGGED_IN));
            response.setContentType("application/json;charset=UTF-8");
            context.setResponseBody(str);
            context.setResponseStatusCode(HttpStatus.SC_FORBIDDEN);
        }else{
            System.out.println("===========");
            // 校验
            try {
                // 校验通过什么都不做，即放行
                UserInfo userInfo= JwtUtil.getInfoFromToken(token, this.jwtProperties.getPublicKey());
                System.out.println(userInfo);
            } catch (Exception e) {
                // 校验出现异常，返回403
                System.out.println("run 方法出现异常");
                e.printStackTrace();
                context.setSendZuulResponse(false);
                String str= JSON.toJSONString(ResponseBean.failure(ResultCode.USER_NOT_LOGGED_IN));
                response.setContentType("application/json;charset=UTF-8");
                context.setResponseBody(str);

                context.setResponseStatusCode(HttpStatus.SC_FORBIDDEN);
            }
        }

        return null;
    }
}
